package algorithm.greedy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-07 16:50
 * @ Description :
 */
public class GreedyAlgo
{
    public static void main(String[] args)
    {
        HashMap<String, HashSet> broadCasts = new HashMap<>();
        HashSet<String> hashSet1 = new HashSet<>();
        hashSet1.add("北京");
        hashSet1.add("上海");
        hashSet1.add("天津");

        HashSet<String> hashSet2 = new HashSet<>();
        hashSet2.add("广州");
        hashSet2.add("北京");
        hashSet2.add("深圳");

        HashSet<String> hashSet3 = new HashSet<>();
        hashSet3.add("成都");
        hashSet3.add("上海");
        hashSet3.add("杭州");

        HashSet<String> hashSet4 = new HashSet<>();
        hashSet4.add("上海");
        hashSet4.add("天津");

        HashSet<String> hashSet5 = new HashSet<>();
        hashSet5.add("杭州");
        hashSet5.add("大连");

        broadCasts.put("K1", hashSet1);
        broadCasts.put("K2", hashSet2);
        broadCasts.put("K3", hashSet3);
        broadCasts.put("K4", hashSet4);
        broadCasts.put("K5", hashSet5);

        HashSet<String> allAreas = new HashSet<>();
        allAreas.add("北京");
        allAreas.add("上海");
        allAreas.add("广州");
        allAreas.add("深圳");
        allAreas.add("成都");
        allAreas.add("杭州");
        allAreas.add("大连");

        //存放选择的电台
        ArrayList<String> selects = new ArrayList<>();

        //临时集合,存放遍历过程中电台覆盖的地区与还未覆盖地区的交集
        HashSet<String> tempSet = new HashSet<>();

        String maxKey = null;

        while (allAreas.size() != 0)
        {
            maxKey = null;

            //选出最大覆盖的key
            for (String key : broadCasts.keySet())
            {
                tempSet.clear();

                //取出当前key(广播)能够覆盖的地区
                HashSet<String> areas = broadCasts.get(key);
                tempSet.addAll(areas);

                //求交集
                tempSet.retainAll(allAreas);

                //贪心
                if (tempSet.size() > 0 &&
                        (maxKey == null ||
                                tempSet.size() > broadCasts.get(maxKey).size()))
                {
                    maxKey = key;
                }
            }

            if (maxKey != null)
            {
                selects.add(maxKey);
                allAreas.removeAll(broadCasts.get(maxKey));
            }
        }

        System.out.println("选择结果: " + selects);

    }


}
